#define _CRT_SECURE_NO_WARNINGS 
#include"Stack.h"

void InitST(ST* ps)
{
	ps->arr = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
//push
void STpush(ST* ps,STtype x)
{
	if (ps->top == ps->capacity)
	{
		//kuo rong
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STtype* temp = realloc(ps->arr, newcapacity * sizeof(STtype));
		if (temp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}
	ps->arr[ps->top++] = x;
}
//isempty
bool STisEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//pop
void STpop(ST* ps)
{
	assert(!STisEmpty(ps));
	--ps->top;
}
//return top
STtype STTop(ST* ps)
{
	assert(!STisEmpty(ps));
	return ps->arr[ps->top - 1];
}
void STdestroy(ST* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
		ps->arr = NULL;
	}
}